package com.erp.module.article

import android.content.Context
import com.erp.Interface.OnUserDataInterface
import com.erp.bean.ArticleBean
import com.erp.bean.ArticleCategoryBean
import com.erp.pull.BasePullP
import com.erp.util.JsonUtil
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject

/**
 * Created by Administrator on 2019/3/30/030.
 * 作者：刘洋
 */
class IArticleP(var list : MutableList<ArticleBean>,var V : ArticleContract.ArticleView,var context: Context): BasePullP(),ArticleContract.ArticleP {

    var acbean = ArticleBean.ArticleSouSuo()
    var md = ArticleModel()
    var listCategory = ArrayList<ArticleCategoryBean>()
    init {
        PullV = V
    }
    override fun getData(page: Int, pagesize: Int, onUserDataInterface: OnUserDataInterface) {
        md.Article(context,page,pagesize,acbean,onUserDataInterface)
    }

    override fun jsondata(data: String?) {
        if(data==null){
            return
        }
        try {
            var js = JSONArray(data)
            for (i in 0 until js.length()){
                var jo = js.get(i) as JSONObject
                var bean = ArticleBean()
                bean.title = JsonUtil.getvolue(jo,"title")
                bean.hits = JsonUtil.getvolue(jo,"hits")
                bean.content = JsonUtil.getvolue(jo,"content")
                bean.ctime = JsonUtil.getvolue(jo,"ctime")
                bean.uptime = JsonUtil.getvolue(jo,"uptime")
                bean.kac_category = JsonUtil.getvolue(jo,"kac_category")
                bean.kac_category1 = JsonUtil.getvolue(jo,"kac_category1")
                bean.kac_category2 = JsonUtil.getvolue(jo,"kac_category2")
                list.add(bean)
            }
        }catch (e : JSONException){
            e.printStackTrace()
        }
    }

    override fun listClean() {
        list.clear()
    }

    override fun sousuo(acbean : ArticleBean.ArticleSouSuo) {
        this.acbean=acbean
        var id =getid(acbean.category,listCategory)
        var list1 : MutableList<String> = ArrayList<String>()
        for (bean in listCategory){
            if(bean.pid==id){
                list1.add(bean.id)
            }
        }
        var list2 : MutableList<String> = ArrayList<String>()
        for(bean in listCategory){
            for (d in list1){
                if (bean.pid == d){
                    list2.add(bean.id)
                }
            }
        }
        acbean.c.add(id)
        acbean.c.addAll(list1)
        acbean.c.addAll(list2)
        xiala()
    }

    override fun getArticleCategory() {
        md.getArticleCategory(context,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
                if(data==null){
                    return
                }
                ArticleCategoryJson(data)
                V.setSpSuperiorSection(data)
            }

            override fun onNot(throwable: String?) {
            }

        })
    }

    fun ArticleCategoryJson(data : String?){
        if(data==null){
            return
        }
        try{
            var js  = JSONArray(data)
            for (i in 0 until js.length()){
                val jo = js[i] as JSONObject
                var bean = ArticleCategoryBean()
                bean.id = JsonUtil.getvolue(jo,"id")
                bean.name = JsonUtil.getvolue(jo,"name")
                bean.description = JsonUtil.getvolue(jo,"description")
                bean.pid = JsonUtil.getvolue(jo,"pid")
                listCategory.add(bean)
            }
        }catch (e : JSONException){
            e.printStackTrace()
        }
    }


    fun getid(s : String ,list : ArrayList<ArticleCategoryBean>) : String{
        var id =""
        for(bean in list){
            if(bean.name == s){
                id = bean.id
                break
            }
        }
        return id
    }
}